package com.bawei.mall.cart.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bawei.common.core.domain.R;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.cart.domain.MallProductCart;
import com.bawei.mall.cart.domain.reponse.MallProductCartVo;
import com.bawei.mall.cart.service.MallProductCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author：作者姓名
 * @Package：com.bawei.mall.cart.controller
 * @Project：mall_cloud
 * @name：MallProductCart
 * @Date：2024/7/5 14:59
 */
@RestController
@RequestMapping("/Cart")
public class MallProductCartController {

    @Autowired
    private MallProductCartService mallProductCartService;

    @GetMapping("/list")
    public R<List<MallProductCartVo>> mallProductCartList(){

        /*List<MallProductCart> list = mallProductCartService.list();
        LambdaQueryWrapper<MallProductCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallProductCart::getId,1);
        List<MallProductCart> list1 = mallProductCartService.list(wrapper);
        MallProductCart one = mallProductCartService.getOne(wrapper);
        MallProductCart productCart = mallProductCartService.getById(1);
        mallProductCartService.remove(new LambdaQueryWrapper<>());
        LambdaUpdateWrapper<MallProductCart> updateWrapper = new LambdaUpdateWrapper<>();
        mallProductCartService.update();
        wrapper.groupBy(MallProductCart::getId);
*/
        return R.ok(mallProductCartService.selectmallProductCartList());
    }

    @PostMapping("/add")
    public R mallProductCartAdd(@RequestBody MallProductCart mallProductCart){
        Long userId = SecurityUtils.getUserId();
        mallProductCart.setMemberId(userId);
        mallProductCart.setUpdateDate(new Date());
        return R.ok(mallProductCartService.save(mallProductCart));
    }

    @GetMapping("/quantity")
    public R mallProductCartUpd(@RequestParam Integer id,@RequestParam Integer quantity){
        LambdaUpdateWrapper<MallProductCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MallProductCart::getId,id)
                .set(MallProductCart::getQuantity,quantity)
                .set(MallProductCart::getUpdateDate,new Date());
        return R.ok(mallProductCartService.update(updateWrapper));
    }

    @PostMapping("/delete")
    public R mallProductCartDel(@RequestParam Integer ids){
        return R.ok(mallProductCartService.removeById(ids));
    }

    @PostMapping("/clear")
    public R mallProductCartClear(){
        mallProductCartService.mallProductCartClear();
        return R.ok();
    }








}
